<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>

<title>JRuby Tutorial: Ruby Study Notes - Best Ruby Guide, Ruby Tutorial</title>

<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="description" content="Ruby Study Notes - Best Ruby Guide, Ruby Tutorial" />
<meta name="keywords" content="jruby,jruby tutorial,tutorial,ruby,java" />
<meta name="Distribution" content="Global" />
<meta name="author" content="Satish Talim / Original design: Erwin Aligam - ealigam@gmail.com" />
<meta name="copyright" content="Satish Talim 2007 and beyond..." />
<meta name="verify-v1" content="rFu86se+IkbtF+bH8mgJBKwU5HnKaSd8Ghw9umXQOkM=" />
<meta name="robots" content="index,follow" />
<meta http-equiv="Expires" content="0" />
<meta name="revisit-after" content="1 days" />

<link rel="stylesheet" href="/images/NewOrange.css" type="text/css" />
<link rel="stylesheet" href="/images/syntaxhighlighter.css" type="text/css" />
<!-- Google Analytics code -->
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-59044-10']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
<!-- Google Analytics code ends -->
</head>

<body>
<!-- wrap starts here -->
<div id="wrap">

    <div id="header">

        <h1 id="logo">Ruby<span class="orange">Learning.github.io</span></h1>
        <h2 id="slogan">Ruby helps programmers have more fun!</h2>

    </div>

    <div id="menu">
        <ul>
            <li><a href="/" title="Home page for rubylearning.github.io">Home</a></li>
            <li id="current"><a href="/satishtalim/tutorial.html" title="Get started Learning Ruby here...">Tutorial</a></li>
            <li><a href="/download/downloads.html" title="Download this tutorial as an eBook">Downloads</a></li>
            <li><a href="/other/testimonials.html" title="People around the world who benefited from this site">Testimonials</a></li>
            <li><a href="/other/certification.html" title="Get certified in Ruby">Certification</a></li>
            <li><a href="/satishtalim/ruby_guide.html" title="Ruby Guide, Mentor">Mentor</a></li>
            <li><a href="/blog/" title="Ruby blog of Learning Ruby site">Blog</a></li>
            <li><a href="/satishtalim/tutorial.html" title="Online Ruby Course">Online Course</a></li>
            <li><a href="http://ruby-challenge.rubylearning.org/" title="Ruby Programming Challenge for Newbies">Challenge</a></li>
            <li><a href="/satishtalim/about.html" title="Information about Satish Talim">About</a></li>
        </ul>
    </div>

    <!-- content-wrap starts here -->
    <div id="content-wrap">

            <div id="main">

                <div id="main-inner"><a name="TemplateInfo"></a>
                <h1>JRuby Tutorial</h1>

                <p>Recently, <a href="http://jruby.codehaus.org/" >JRuby</a> has been gaining more and more attention in the Java and Ruby communities. Java is a powerful platform and there are millions of lines of Java code being written each month, that the world will have to live with for a long time from now. By leveraging Java the platform with the power of the Ruby programming language, programmers get the best from both worlds. You better not ignore JRuby any more!</p>

                <h3>What is JRuby?</h3>

                <p>JRuby is a 100% pure-Java implementation of the Ruby programming language. JRuby's creators, <a href="http://www.bloglines.com/blog/ThomasEEnebo" >Thomas Enebo</a> and <a href="/blog/2007/04/26/interview-charles-nutter/" >Charles Nutter</a>, have been hired by <a href="http://www.sun.com/" >Sun</a> to work on JRuby full time. The current JRuby release 1.0.3 is fully compatible with Ruby 1.8.5.</p>

                <p><a href="http://ola-bini.blogspot.com/" >Ola Bini</a> says that "JRuby is ready for prime time. Application developers should try their applications on JRuby NOW."</p>

                <p>JRuby, as you will realize, is very easy to use. Here's a small <strong>JRuby Tutorial</strong> for you.</p>

                <h3>Download and Setup:</h3>

                <ul><li>The <a href="http://dist.codehaus.org/jruby/" >JRuby distribution</a> comes as a .zip file (for Windows), namely jruby-bin-1.0.3.zip</li><li>Uncompress the archive; you should end up with a <strong>jruby-1.0.3 folder</strong>.</li><li>In Windows, set the system environment variable <strong>JRUBY_HOME</strong> to C:/jruby-1.0.3 I am assuming that you have uncompressed JRuby to C:</li><li>Also, set the system environment variable <strong>path</strong> to C:/jruby-1.0.3/bin;</li><li>The JRuby distribution's bin directory contains the jruby.bat file that is used to run the JRuby interpreter. Run the command <strong>jruby -version</strong> from the command line to test that the JRuby is working. On my PC, it said:<br /><strong>ruby 1.8.5 (2007-12-15 rev 5200) [x86-jruby1.0.3]</strong></li></ul>

                <h3>Where to use JRuby?</h3>

                <p>a. <strong><i>JRuby allows Ruby programs to use Java classes</i></strong>. This is a powerful concept that JRuby now brings to Ruby users. My <a href="/satishtalim/ruby_tk_tutorial.html" >Ruby/Tk Tutorial</a> has a program <strong>p075hellotk1.rb</strong> that uses Ruby/Tk a graphical user interface. However, the documentation for Ruby/Tk is extremely poor and I would be comfortable in using Java Swing instead. The code in JRuby for the program <strong>p075hellotk1.rb</strong> can be something like this:</p>

                <div class="column2">
                <!-- InstanceBeginEditable name="Code" -->
                <textarea name="code" class="ruby:nogutter:nocontrols" rows="15" cols="60">
                # javaSwingHello.rb
                require 'java' # Line 2
                JFrame = javax.swing.JFrame
                JLabel = javax.swing.JLabel
                frame  = JFrame.new
                jlabel = JLabel.new("Hello World")
                frame.add(jlabel)
                frame.setDefaultCloseOperation(JFrame::EXIT_ON_CLOSE)
                frame.pack
                frame.setVisible(true)
                </textarea>
                <!-- InstanceEndEditable -->
                </div>

                <p>Run the above program from the command line as follows:<br /><strong>jruby javaSwingHello.rb</strong><br /><br />Note: Line 2: The second line of the above program enables JRuby's Java support and allows a Ruby program to use Java classes.</p>

                <p>b. <strong><i>Calling JRuby from Java</i></strong>. JRuby can just as easily be called from Java, using either the JSR 223 Scripting for Java 6 or the <a href="http://en.wikipedia.org/wiki/Bean_Scripting_Framework" >Apache Bean Scripting</a> framework. More information on this is available in the <a href="http://www.headius.com/jrubywiki/index.php/Main_Page" >JRuby Wiki</a></p>

                <p>c. <strong><i>Running Rails with JRuby</i></strong>. The advantages are obvious. To quote the JRuby Wiki</p>

                <ul>
                <li>Gives Rails the power and functionality of Java: Virtual Machine, application servers, and libraries.</li>
                <li>With future JVM and JRuby improvements, JRuby may be faster than Matz's Ruby Interpreter in running Rails</li>
                <li>Gets Rails in the door of Java shops by making Rails apps into Java-platform apps.</li>
                </ul>

                <p>I personally feel that if you have a Java background and are now exploring Ruby, you should definitely explore JRuby further.</p>

                <h3>Resources:</h3>

                <ul><li><a href="http://jruby.codehaus.org/" >JRuby Home page</a></li>
                <li><a href="http://www.headius.com/jrubywiki/index.php/Main_Page" >JRuby Wiki</a></li><li><a href="http://www.headius.com/jrubywiki/index.php/JRuby_on_Rails" >Rails on JRuby</a></li><li><a href="http://www.nabble.com/JRuby---User-f14107.html" >JRuby Forum</a></li><li><a href="http://java.sun.com/developer/technicalArticles/scripting/jruby/" >JRuby and the Java platform</a></li></ul>

                <h3>Useful Articles:</h3>

                <ul><li><a href="/blog/2007/04/30/internationalization-in-jruby/" >Internationalization in JRuby</a></li><li><a href="/blog/2007/05/05/connect-jruby-to-mysql-using-jdbc/" >Connect JRuby to MySQL using JDBC</a></li><li><a href="/blog/2007/08/30/interview-with-ola-bini-jruby-core-developer/">Adoption of JRuby - Ola Bini</a></li></ul>

                <p class="post-footer align-right">
                  <strong>
                    <a href="http://sitekreator.com/satishtalim/java_to_ruby.html">&lt;Java to Ruby | </a>
                    <a href="/satishtalim/tutorial.html">TOC | </a>
                    <a href="http://sitekreator.com/satishtalim/solutions.html">Solutions&gt;</a>
                  </strong>
                </p>

            </div>
            <!-- main inner ends here -->
        </div>

            <div id="rightbar">

            </div>

    <!-- content-wrap ends here -->
    </div>

<!-- wrap ends here -->
</div>

<!-- footer starts here -->
<div id="footer">
    <!-- CHANGE THE FOOTER -->
    <p>&copy; 2006-2021 <strong>rubylearning.github.io - A Ruby Tutorial</strong>&nbsp;&nbsp;Page Updated: 5th Jan. 2021 | Design: <a href="mailto:ealigam@gmail.com">Erwin Aligam</a> | Valid: <a href="http://validator.w3.org/check/referer">XHTML</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="/">Home</a> | <a href="/privacy.html">Privacy</a> | <a href="/sitemap.html">Sitemap</a></p>
    </p>
</div>
<!-- footer ends here -->

<!-- Adsense: RLJRT -->

<!-- SyntaxHighlighter code -->
<script src="/js/shCore.js" type="text/javascript"></script>
<script src="/js/shBrushRuby.js" type="text/javascript"></script>
<script type="text/javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>
<!-- SyntaxHighlighter code -->

</body>
</html>
